var pb = null;
var jSonRequest = null;

function sendNewsletters(form)
{
	if (pb === null) {
		pb = new ProgressBar(0,{
			'width':200,
			'height':20
		});
		$('wrapper').appendChild(pb);
		$('wrapper').setStyle('display', 'block');
	}
	
	var options = {
		onComplete: function(response){
			
			if(response.error){
				alert(response.error);
				return;
			} 
			
			if( response.nextlimitstart < response.total ) {
				
				pb.setValue(response.percent);
				jSonRequest = new Json.Remote( form.action+'&format=raw' , options );
				jSonRequest.send({'mailfrom': form.mailfrom.value, 
                           'replyto': form.replyto.value, 
                           'only_confirmed': form.only_confirmed.checked,
                           'textformat': form.textformat.checked,
                           'limitstart': response.nextlimitstart});
      
			} else {
				pb.setValue(100);
				alert(response.message);
			}
		}
	};
	
	if (jSonRequest === null) {
		jSonRequest = new Json.Remote( form.action+'&format=raw' , options );
	}
	
	jSonRequest.send({'mailfrom': form.mailfrom.value, 
                      'replyto': form.replyto.value, 
                      'only_confirmed': form.only_confirmed.checked,
                      'textformat': form.textformat.checked });
	return false;
}
